﻿/**********************************************************
 ●项目：Smartkernel
 ●公司：智能核心信息技术研发中心
 ●作者：曹艳白
 ●邮箱：caoyanbai@gmail.com
 ●创建：2008-08-10
 ●版本：1.0
**********************************************************/
using Smartkernel.Exceptions;
using Smartkernel.Extensions;
using Smartkernel.Resource;
namespace Smartkernel.Caching
{
    /// <summary>
    /// 缓存器的键
    /// </summary>
    /// <typeparam name="TKey">客户端程序员所使用的键值类型，注意，如果是自定义类型作为键值，必须正确实现了Equals和GetHashCode方法</typeparam>
    public partial class SmartCacherKey<TKey>
    {
        #region 属性、字段和构造函数
        private TKey key;
        /// <summary>
        /// 原始键
        /// </summary>
        public TKey Key
        {
            get
            {
                return this.key;
            }
        }
        private string category;
        /// <summary>
        /// 缓存所属的分类
        /// </summary>
        public string Category
        {
            get
            {
                return this.category;
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="key">原始键</param>
        /// <param name="category">缓存所属的分类</param>
        public SmartCacherKey(string category, TKey key)
        {
            if (category.IsNullOrEmptyOrBlank())
            {
                throw new SmartException(SmartExceptionMessageResource.SmartCacherKey_category_NullOrEmptyOrBlank);
            }
            if (key == null)
            {
                throw new SmartException(SmartExceptionMessageResource.SmartCacherKey_key_Null);
            }
            if (key is string)
            {
                if (key.ToString().IsNullOrEmptyOrBlank())
                {
                    throw new SmartException(SmartExceptionMessageResource.SmartCacherKey_key_NullOrEmptyOrBlank);
                }
                key = SmartReflection.ConvertTo<TKey>(key.ToString().Trim());
            }

            this.key = key;
            this.category = category.Trim();
        }
        #endregion
    }
    public partial class SmartCacherKey<TKey>
    {
        #region 覆盖基类的方法
        /// <summary>
        /// 比较两个对象是否相等
        /// </summary>
        /// <param name="obj">要比较的对象</param>
        /// <returns>比较的结果</returns>
        public override bool Equals(object obj)
        {
            SmartCacherKey<TKey> other = obj as SmartCacherKey<TKey>;
            if (other == null)
            {
                return false;
            }
            return this.Category.Equals(other.Category) && this.Key.Equals(other.Key);
        }
        /// <summary>
        /// 获得哈希码
        /// </summary>
        /// <returns>哈希码</returns>
        public override int GetHashCode()
        {
            return this.ToString().GetHashCode();
        }
        /// <summary>
        /// 获得字符串表示
        /// </summary>
        /// <returns>输出的字符串</returns>
        public override string ToString()
        {
            return "SmartCacherKey__" + this.Category + "__" + this.Key.GetHashCode().ToString();
        }
        #endregion
    }
}
